<template>
  <div class="loginPage">
    <div class="welcome">
      <h1>欢迎使用</h1>
      <h2>中咨集团科技与数字化管理平台</h2>
      <img src="./img/login.png" alt="" />
    </div>
    <div class="right">
      <div class="box">
        <div class="logo">
          <img src="./img/logo.png" alt="" />
          <div class="way">用户名登录</div>
          <img class="line" src="./img/line.png" alt="" />
        </div>
        <el-form ref="loginDom" :rules="rules" :model="data.userAccount">
          <el-form-item prop="username">
            <el-input v-model="data.userAccount.username" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input @keyup.enter="onSubmit" type="password" show-password v-model="data.userAccount.password" />
          </el-form-item>
          <el-form-item class="btnForm">
            <el-button
              :disabled="data.loginLoading"
              v-loading="data.loginLoading"
              class="btn"
              type="primary"
              @click="onSubmit"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { getUser } from '@/api/global'
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { userLogin } from './api/login'
import { servicesLoading } from '@/utils/loading'
import { ElMessage } from 'element-plus'
import { forEach } from 'lodash'

const router = useRouter()
const store = useStore()

const loginDom = ref()
let data = reactive({
  userAccount: {
    username: '',
    password: '',
  },
  loginLoading: false,
})
const rules = {
  username: [
    {
      required: true,
      message: '请输入用户名',
    },
  ],
  password: [
    {
      required: true,
      message: '请输入密码',
    },
  ],
}

const onSubmit = () => {
  loginDom.value.validate((valid: any) => {
    if (valid) {
      data.loginLoading = true
      userLogin(data.userAccount)
        .then((res) => {
          if (res.data.code === 200) {
            ElMessage({
              message: '登录成功！',
              type: 'success',
            })
            localStorage.setItem('token', res.data.data)
            router.replace('/')
            data.loginLoading = false
            getUser().then((res) => {
              localStorage.setItem('permissions', JSON.stringify(res.data.data.authority))
              localStorage.setItem('userInfo', JSON.stringify(res.data.data.sysUser))
              store.state.system.userInfo = res.data.data.sysUser
            })
            const loading = servicesLoading({ fullscreen: false, text: '正在加载路由菜单' })
            store
              .dispatch('GenerateRoutes')
              .then((accessRoutes) => {
                console.log(accessRoutes, 'router.getRoutes()')
                if (!accessRoutes.length) {
                  ElMessage({
                    message: '该用户没有任何菜单权限！',
                    type: 'warning',
                  })
                  localStorage.removeItem('token')
                  localStorage.removeItem('userInfo')
                  localStorage.removeItem('permissions')
                  localStorage.removeItem('routes')
                  localStorage.removeItem('zhongziConfig')

                  store.state.router.routes = []
                  router.push('/login')
                  return
                }
                // 根据roles权限生成可访问的路由表
                forEach(accessRoutes, (route) => {
                  // console.log(route, 'route')
                  router.addRoute('home', route) // 动态添加可访问路由表
                })
                router.replace('/')
                loading.close()
              })
              .catch((error) => {
                loading.close()
                console.error(error)
              })
          } else {
            ElMessage({
              message: '登录失败！',
              type: 'error',
            })
            data.loginLoading = false
          }
        })
        .catch(() => {
          data.loginLoading = false
        })
    } else {
      console.log('error submit!')
      return false
    }
  })
}
</script>

<style scoped lang="scss">
.loginPage {
  display: flex;
  height: 100vh;

  .welcome {
    width: 500px;
    color: #3e8fff;
    box-sizing: border-box;
    padding: 150px 50px;
    background-color: #e7f1ff;

    img {
      width: 400px;
      margin: 20px 0;
    }
  }

  .right {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;

    .logo {
      text-align: center;
      margin-bottom: 30px;

      .way {
        color: #3e8fff;
        font-size: 20px;
        margin-top: 20px;
      }

      .line {
        width: 20px;
      }
    }

    .box {
      width: 300px;
      margin: auto;
      padding: 0 20px;
      border-radius: 20px;

      .el-form-item {
        .btn {
          width: 100%;
          margin-top: 10px;
        }

        :deep(.el-form-item__label) {
          width: 100px;
          color: rgb(22, 27, 27);
        }
      }
    }
  }
}
</style>
